import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
// import { FarmPlantpower } from "../module/monitor/main/plan-power-panel/farm-plantpower.module";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/subject";
import { FarmPlantpower } from "../entity/farm-plant-power.model";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class FarmPlantPowerService {
    private readonly param = "plantPowers";
    private get url(): string {
        return `${AppConfig.APP_RESTFUL}/${this.param}`;
    }

    constructor(private http: InterceptorService,
        private http2: Http) { }
    getFarmPlantPowers(): Promise<FarmPlantpoweTotal> {
        let url = `${this.url}/plantPower`;
        return this.http.get(url)
            .toPromise()
            .then(response => {
                return response.json().data as FarmPlantpoweTotal
            })
            .catch(this.handleError);
    }
    getFarmPlantPowerList(): Promise<FarmPlantpower[]> {
        return this.http.get(this.url)
            .toPromise()
            .then(response => {
                return response.json().data as FarmPlantpower[]
            })
            .catch(this.handleError);
    }

    delete(id: number): Promise<any> {
        const url = `${this.url}/plantPowerId/${id}`;
        return this.http.delete(url)
            .toPromise()
            .then(res => {
                let result = res.json();
                switch (result.code) {
                    case "200":
                        return result;
                    case "-1":
                        throw new Error(result.message);
                }
            })
            .catch(this.handleError);
    }

    create(farmPlantpower: FarmPlantpower): Promise<FarmPlantpower> {
        // const url = `http://192.168.15.150:8585/webapi/v1/plantPowers`;
        return this.http.post(this.url, JSON.stringify(farmPlantpower))
            .toPromise()
            .then(res => {
                try {
                    let result = res.json();
                    switch (result.code) {
                        case "200":
                            return result.data;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    this.handleError(error);
                }
            })
            .catch(this.handleError);
    }
    update(farmPlantpower: FarmPlantpower): Promise<FarmPlantpower> {
        const url = `${this.url}`;
        return this.http.put(url, JSON.stringify(farmPlantpower))
            .toPromise()
            .then(() => farmPlantpower)
            .catch(this.handleError);
    }


    hasFarmPlantpower(farmId: number, plantType: string, datePlant: string): Promise<boolean> {
        // const url = `http://192.168.15.150:8585/webapi/v1/plantPowers/getBasicPlantPowerByTypes`;
        const url = `${this.url}/getBasicPlantPowerByTypes`;
        return this.http.post(url, JSON.stringify({ 'farmId': farmId, 'plantType': plantType, 'datePlant': datePlant }))
            .toPromise()
            .then(res => {
                try {
                    let result = res.json();
                    switch (result.code) {
                        case "3001":
                            return true;
                        case "3002":
                            return false;
                    }
                }
                catch (error) {
                    this.handleError(error);
                }
            })
            .catch(this.handleError);

    }
    importPlannedPower(file) {
        let formData = new FormData();
        formData.append('file', file[0], file[0].name);
        const url = `${this.url}/importPlannedPower`;
        return this.http2.post(url, formData)
          .toPromise()
          .then(res =>  res.json())
          .catch(this.handleError);
      }
    private handleError(error: any): Promise<any> {
        return Promise.reject(error.message || error);
    }

}
interface FarmPlantpoweTotal {
    yearList: FarmPlantpower[],
    dayList: FarmPlantpower[],
    monthList: FarmPlantpower[]
}


// WEBPACK FOOTER //
// ./src/app/service/farm-plant-power.service.ts